import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "index",
      component: () => import("../view/security/login.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../view/security/register.vue"),
    },
    {
      path: "/phoneLogin",
      name: "phoneLogin",
      component: () => import("../view/security/phoneLogin.vue"),
    },
    {
      path: "/main",
      name: "main",
      component: () => import("../view/home/index.vue"),
      children: [
        {
          path: "userMeeting",
          name: "userMeeting",
          components: {
            main: () => import("@/view/user/meeting/dashboard.vue"),
          },
        },
        {
          path: "student",
          name: "student",
          components: {
            main: () => import("@/view/student/index.vue"),
          },
          children: [
            {
              path: "account",
              name: "studentAccount",
              components: {
                view: () => import("@/view/student/account.vue"),
              },
            },
          ],
        },
        {
          path: "uniStudent",
          name: "uniStudent",
          components: {
            main: () => import("@/view/uni/student/index.vue"),
          },
          children: [
            {
              path: "account",
              name: "uniStudentAccount",
              components: {
                view: () => import("@/view/uni/student/account.vue"),
              },
            },
          ],
        },
        {
          path: "teacher",
          name: "teacher",
          components: {
            main: () => import("@/view/teacher/index.vue"),
          },
          children: [
            {
              path: "account",
              name: "teacherAccount",
              components: {
                view: () => import("@/view/teacher/account.vue"),
              },
            },
            {
              path: "department",
              name: "teacherDepartment",
              components: {
                view: () => import("@/view/teacher/department.vue"),
              },
            },
          ],
        },
        {
          path: "uniTeacher",
          name: "uniTeacher",
          components: {
            main: () => import("@/view/uni/teacher/index.vue"),
          },
          children: [
            {
              path: "account",
              name: "uniTeacherAccount",
              components: {
                view: () => import("@/view/uni/teacher/account.vue"),
              },
            },
            {
              path: "department",
              name: "uniTeacherDepartment",
              components: {
                view: () => import("@/view/uni/teacher/department.vue"),
              },
            },
          ],
        },
        {
          path: "uniMeeting",
          name: "uniMeeting",
          components: {
            main: () => import("@/view/uni/meeting/index.vue"),
          },
          children: [
            {
              path: "room",
              name: "uniRoom",
              components: {
                view: () => import("@/view/uni/meeting/room.vue"),
              },
            },
            {
              path: "application",
              name: "uniApplication",
              components: {
                view: () => import("@/view/uni/meeting/application.vue"),
              },
            },
          ],
        },
        {
          path: "uniPermission",
          name: "uniPermission",
          components: {
            main: () => import("@/view/uni/permission/index.vue"),
          },
          children: [
            {
              path: "manage",
              name: "uniManage",
              components: {
                view: () => import("@/view/uni/permission/manage.vue"),
              },
            },
          ],
        },
        {
          path: "meeting",
          name: "meeting",
          redirect: "/main/meeting/dashboard",
          components: {
            main: () => import("@/view/meeting/index.vue"),
          },
          children: [
            {
              path: "dashboard",
              name: "dashboard",
              components: {
                view: () => import("@/view/meeting/dashboard.vue"),
              },
            },
            {
              path: "room",
              name: "room",
              components: {
                view: () => import("@/view/meeting/room.vue"),
              },
            },
          ],
        },
        {
          path: "university",
          name: "university",
          components: {
            main: () => import("@/view/university/index.vue"),
          },
          children: [
            {
              path: "manage",
              name: "manage",
              components: {
                view: () => import("@/view/university/manage.vue"),
              },
            },
          ],
        },
      ],
    },
    {
      path: "/zego",
      name: "zego",
      component: () => import("../view/zego/index.vue"),
    },
    {
      path: "/file",
      name: "file",
      component: () => import("../view/file/index.vue"),
    },
  ],
});

// 开启路由护卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.token;
  if (to.path === "/") {
    next();
  } else isLogin ? next() : next("/");
});

export default router;
